<?php

class CloudSyncAttachmentModel
{
    private $cloudSyncAttachmentKeyPrefix   =   'CSA/';

    public function cloudSync($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) || empty($cloudInfo['type'])   ||  empty($cloudInfo['bodyId']) ||  empty($cloudInfo['attachment'][0]))
        {
            AC::$errorNo    =   'CSAM005';
            return  FALSE;
        }
        $checkType  =   $this->checkType($cloudInfo['type']);
        if (empty($checkType))  return  FALSE;

        $data['uid']        =   $cloudInfo['uid'];
        $data['type']       =   $cloudInfo['type'];
        $data['bodyId']     =   $cloudInfo['bodyId'];
        $data['uploadTime'] =   AC::SERVER_TIME;

        $key    =   $this->cloudSyncAttachmentKeyPrefix. $cloudInfo['uid'];
        foreach ($cloudInfo['attachment']   as  $path)
        {
            if (empty($path))   continue;
            if(is_array($path) && !empty($path["path"])){
                $data['attachmentPath'] =   strval($path["path"]);
                $data['attachmentType'] =   intval($path["type"]);
            }else{
                $data['attachmentPath'] =   $path;
            }
            Storage::addData($key, $data);
        }
        return  TRUE;
    }/*}}}*/

    public function getCloudAttachment($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) ||  empty($cloudInfo['bodyId']))
        {
            AC::$errorNo    =   'CSAM002';
            return  FALSE;
        }

        $key    =   $this->cloudSyncAttachmentKeyPrefix. $cloudInfo['uid'];
        $where  =   array('bodyId' => $cloudInfo['bodyId']);
        $result =   Storage::getConditionManyData($key, $where);

        return  $result;
    }/*}}}*/

    public function cloudUpdate($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) || empty($cloudInfo['type'])   ||  empty($cloudInfo['bodyId']))
        {
            AC::$errorNo    =   'CSAM001';
            return  FALSE;
        }
        $checkType  =   $this->checkType($cloudInfo['type']);
        if (empty($checkType))  return  FALSE;

        $this->cloudRemove($cloudInfo);
        if (!empty($cloudInfo['attachment'][0]))    $this->cloudSync($cloudInfo);

        return  TRUE;
    }/*}}}*/

    public function cloudRemove($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) ||  (empty($cloudInfo['bodyId']) && empty($cloudInfo['bodyIds'])))
        {
            AC::$errorNo    =   'CSAM001';
            return  FALSE;
        }
        # $checkType  =   $this->checkType($cloudInfo['type']);
        # if (empty($checkType))  return  FALSE;

        $key    =   $this->cloudSyncAttachmentKeyPrefix. $cloudInfo['uid'];
        if (empty($cloudInfo['bodyIds']))
        {
            $where  =   array('uid' => $cloudInfo['uid'], 'bodyId' => $cloudInfo['bodyId']);
        }
        else
        {
            $where  =   ' bodyId IN ('. $cloudInfo['bodyIds']. ') AND uid = "'. $cloudInfo['uid']. '" ';
        }
        $result =   Storage::delConditionData($key, $where);
        if (empty($result)) AC::$errorNo    =   'CSAM004';

        return  TRUE;
    }/*}}}*/

    private function checkType($type)/*{{{*/
    {
        return  TRUE;
        $cloudTypeArray =   array(
                                '1'     =>  1,
                                '8'     =>  8,
                                '9'     =>  9,
                                '10'    =>  10,
                                '12'    =>  12,
                                '14'    =>  14,
                                '15'    =>  15,
                            );
        if (empty($cloudTypeArray[$type]))
        {
            AC::$errorNo    =   'CSAM003';
            return  FALSE;
        }
        return  $cloudTypeArray[$type];
    }/*}}}*/

}

